{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from gensim.models.word2vec import Word2Vec\n",
    "import gensim\n",
    "import numpy as np\n",
    "def load_model(filename,is_binary=False):\n",
    "        model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary = is_binary)\n",
    "        return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "    template_file = '../../middle/M2_template.txt'\n",
    "    model_normal_sa = load_model('../../model/M2.model')\n",
    "    template_vector_file = '../../model/M2_template.vector'\n",
    "    template_to_index = {}\n",
    "    index_to_template = {}\n",
    "    template_to_vector = {}\n",
    "    template_num = 373\n",
    "    index = 1\n",
    "    f = open(template_vector_file, 'w')\n",
    "    f.writelines('373 200\\n') #word2vec的模型格式，第一行为单词数&维度\n",
    "    with open(template_file) as IN:\n",
    "        for line in IN:\n",
    "            template = line.strip()\n",
    "            l = template.split()\n",
    "            cur_vector = np.zeros(200)\n",
    "            for word in l:\n",
    "                cur_vector += model_normal_sa[word]\n",
    "            cur_vector /= len(l)\n",
    "            template_to_vector[template] = cur_vector\n",
    "            template_to_index[template] = str(index)\n",
    "            index_to_template[index] = template\n",
    "\n",
    "            f.writelines(str(index))\n",
    "            for v in cur_vector:\n",
    "                f.writelines(' '+str(v))\n",
    "            f.writelines('\\n')\n",
    "            index += 1\n",
    "# template_to_vector['%%10IFNET/5/LINK_UPDOWN: is Line on the interface protocol Vlan UP']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#读取tempalte对应的向量   \n",
    "model_template_index = load_model(template_vector_file) \n",
    "model_list = [model_template_index]#, model_no_sa, model_changed_ant_02, model_changed_a_01, model_a_01_nosyn]\n",
    "row_names = ['有近义词反义词']#,'无近义词反义词','反义词学习率0.2','反义词学习率0.1','反义词学习率0.1无近义词']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/mengweibin/anaconda3/envs/python36/lib/python3.6/site-packages/ipykernel_launcher.py:20: DeprecationWarning: Call to deprecated `wv` (Attribute will be removed in 4.0.0, use self instead).\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>236-266</th>\n",
       "      <th>266-267</th>\n",
       "      <th>266-270</th>\n",
       "      <th>306-307</th>\n",
       "      <th>270-307</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>有近义词反义词</th>\n",
       "      <td>0.112045</td>\n",
       "      <td>0.734338</td>\n",
       "      <td>0.352273</td>\n",
       "      <td>0.694498</td>\n",
       "      <td>0.146322</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          236-266   266-267   266-270   306-307   270-307\n",
       "有近义词反义词  0.112045  0.734338  0.352273  0.694498  0.146322"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "a = template_to_index['%%10IFNET/5/LINK_UPDOWN: is Line on the interface protocol Vlan UP']\n",
    "b = template_to_index['%%10IFNET/3/PHY_UPDOWN: is status link Bridge Aggregation DOWN']\n",
    "c = template_to_index['%%10IFNET/4/LINK is status link Bridge Aggregation DOWN UPDOWN']\n",
    "d = template_to_index['%%10SHELL/6/SHELL_CMD: is the interface Bridge Aggregation up']\n",
    "e = template_to_index['%%10DEV/1/POWER is ID Trap power REMOVED hwPowerRemoved']\n",
    "f = template_to_index['%%10DEV/1/POWER is ID Trap power INSERT hwPowerInserted']\n",
    "# q = 'GigabitEthernet'\n",
    "# w = 'link'\n",
    "# v = 'System'\n",
    "word_list = [a,b,c,d,e,f]\n",
    "# word_list = [a,b,c,d,e,q,w,v]\n",
    "cols = [(a,b),(b,c),(b,d),(e,f),(d,f)]\n",
    "col_name = [ i[0]+'-'+i[1] for i in cols]\n",
    "sim_matirx = []\n",
    "for model in model_list:\n",
    "    temp_list = []\n",
    "    for pair in cols:\n",
    "        sim = model.wv.similarity(pair[0], pair[1])\n",
    "#         print(sim)\n",
    "        temp_list.append(sim)\n",
    "    sim_matirx.append(temp_list)\n",
    "\n",
    "df = pd.DataFrame(sim_matirx, columns = col_name,index=row_names)\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
